<%@ page pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.base.DatabaseObject"%>
<%@page import="com.base.Config"%>
<%@page import="java.math.BigDecimal"%>
<%@include file="inc-header.jsp"%><!-- MAIN CONTENT -->
<div class="box bottom10">
	<%@include file="inc-nav-header.jsp"%>
	<%

	String sPCatId = Utils.getParam(request, "pcatid", "0");
	String sCatId = Utils.getParam(request, "catid", "0");
	String sPage = Utils.getParam(request, "page", "1");
	String sId = Utils.getParam(request, "id", "0");
	DatabaseObject category = contentDAO.getCategory(new BigDecimal(sPCatId));
	DatabaseObject category1 = contentDAO.getCategory(new BigDecimal(sCatId));
	String sClass1 = "class=\"active\"";
	String sClass2 = "class=\"active\"";
	if (category1 != null) sClass1 = "";

	%>
	<div class="tab2">
		<%@include file="dichvu-adv.jsp" %> >>
		<a href="news.jsp?act=dv" <%=category != null?"":"class=\"active\"" %>> Tin tức </a>
		<%if (category != null) {%>
		>> <a href="news-sub.jsp?act=dv&pcatid=<%=category.getAttribute().get("ID") %>" <%=sClass1 %>> <%=category.getAttribute().get("NAME") %></a>
		<%}%>
		<%if (category1 != null) {%>
		>> <a href="news-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=category1.getAttribute().get("ID") %>" <%=sClass2 %>> <%=category1.getAttribute().get("NAME") %></a>
		<%}%>
	</div>
	<!--end .tab2-->
	<!-- BODY CONTENT -->
	<div>
<%
	
	int currPage = Integer.parseInt(sPage);
	int rowsPerPage = 7;
	if (!StringTool.isEmptyOrNul(sCatId)) {
		int totalRecs = contentDAO.countContentByCategory(new BigDecimal(sCatId));
		int numOfPages = (int)((totalRecs % rowsPerPage != 0) ? (totalRecs / rowsPerPage + 1)
				: (totalRecs / rowsPerPage));
		if (totalRecs <= rowsPerPage)
			numOfPages = 1;
		if (totalRecs == 0)
			numOfPages = 0;
		if (currPage > numOfPages)
			currPage = numOfPages;
		if (currPage <= 0)
			currPage = 1;
		long startPos = rowsPerPage * (currPage - 1) + 1;
		long stopPos = startPos + rowsPerPage - 1;
		if (stopPos > totalRecs)
			stopPos = totalRecs;
		long firstPage = 1; // The first page
		long lastPage = numOfPages; // The last page
		long prevPage = currPage - 1;
		if (prevPage < 1) {
			prevPage = 1;
		}
		long nextPage = currPage + 1;
		if (nextPage > numOfPages) {
			nextPage = numOfPages;
		}
	
		ArrayList<DatabaseObject> contentList = contentDAO.getContentListByCategory(new BigDecimal(sCatId), currPage, rowsPerPage);
		if (contentList != null && !contentList.isEmpty()) {%>
		<table cellpadding="0" cellspacing="0" border="1" width="100%">
		<%
			for (DatabaseObject content : contentList) {%>
			<tr>
				<td width="4%" style="padding: 2px;"><a href="news-detail.jsp?act=dv&catid=<%=content.getAttribute().get("category_id") %>&id=<%=content.getAttribute().get("id") %>&page=<%=currPage %>"><img src="<%=imagePath + content.getAttribute().get("preview_image")%>" alt="<%=content.getAttribute().get("name") %>" class="img-45-30"></a></td>
			    <td>
			    <td width="96%" style="vertical-align: top; padding: 5px 2px 5px 4px;">
			        <p><a href="news-detail.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&id=<%=content.getAttribute().get("id") %>&page=<%=currPage %>"><strong> <%=content.getAttribute().get("name") %> </strong></a></p>
			    </td>
			</tr>
			<%}%>
		</table>
		<%
		}%>
		
		<%
	}
	%>
	</div>
	
	<div class="box clearfix">
		<div class="title">
			<h3 class="bg">
				<strong> Danh mục tin tức</strong>
			</h3>
		</div>
		<!--end .title-->
		<ul class="list-dot"><%
		String catParentId = sPCatId;
		if (!StringTool.isEmptyOrNul(catParentId)) {
			ArrayList<DatabaseObject> categoryList = contentDAO.getCategory(catParentId, null);
			for (DatabaseObject cat : categoryList) {%>
			<li>
				<img src="images/icon-list.png" alt="<%=cat.getAttribute().get("NAME") %>">
				<a href="news-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=cat.getAttribute().get("ID") %>"> <%=cat.getAttribute().get("NAME") %> </a>
			</li>
		<%}}%>
		</ul>
	</div>
	<!-- END BODY CONTENT -->
</div>
<!--end .box bottom10-->
<%@include file="inc-nav-bottom.jsp"%>
<!-- END MAIN CONTENT -->
<%@include file="inc-footter.jsp"%>
